package cs.upt.awp.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;

import cs.upt.awp.data.TravelRecord;
import cs.upt.awp.data.User;

public class UsersDAO{
	
	@Autowired
	SessionFactory sessionFactory;

	Session session = null;
	Transaction tx = null;
	
	public User findByUserName(String username) {
		session = sessionFactory.openSession();
		User user = (User) session.load(User.class, new String(username));
		tx = session.getTransaction();
		session.beginTransaction();
		tx.commit();
		return user;
	}


	public boolean addEntity(User user) throws Exception {
		session = sessionFactory.openSession();
		tx = session.beginTransaction();
		session.save(user);
		tx.commit();
		session.close();
		return false;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getEntityList() throws Exception {
		session = sessionFactory.openSession();
		tx = session.beginTransaction();
		List<User> usersList = session.createCriteria(
				TravelRecord.class).list();
		tx.commit();
		session.close();
		return usersList;
	}

	
	public boolean deleteEntity(long id) throws Exception {
		session = sessionFactory.openSession();
		Object o = session.load(TravelRecord.class, id);
		tx = session.getTransaction();
		session.beginTransaction();
		session.delete(o);
		tx.commit();
		return false;
	}


}
